<?php
namespace Manage\Controller;

use Think\Model;

/**
 * Class 店铺管理
 * @package Manage\Controller
 */
class StoreController extends BaseController
{

    /**
     * 店铺管理
     */
    public function index()
    {
        //分页
        $p = max(I('param.p'), 1);
        $num = max(I('param.num'), 20);
        $this->assign('p', $p);
        $this->assign('num', $num);

        //排序
        $order_field = I('param.order_field', 'local_id');
        $order = I('param.order', 'desc');
        $this->assign('order_field', $order_field);
        $sunxu = $order == 'asc' ? 'desc' : 'asc';
        $this->assign('order', $sunxu);

        $order = array($order_field => $order);

        //搜索
        $search_field = I('param.search_field');
        $search = trim(I('param.search'));
        $where[0] = ' local_id <> 26';
        if ($search && $search_field) {
            $where[] = $search_field . " like '%" . $search . "%'";
            $this->assign('search', $search);
            $this->assign('search_field', $search_field);
        }
        //求和
        $count = M('webpos_local_store')
            ->where($where)
            ->count();

        //分页输出
        $Page = new \Think\Page($count, $num);
        $show = $Page->show();

        $fields = array(
            'local_id',
            'local_name',
            'local_number',
            'local_type',
            'local_status',
            'principal',
            'tel',
            'mobile',
        );
        //列表
        $data = M('webpos_local_store')
            ->where($where)
            ->field($fields)
            ->page($p, $num)
            ->order($order)
            ->select();
        if ($data) {
            foreach ($data as &$item) {
                $item['local_status_desc'] = $item['local_status'] ? '正常营业' : '停业';
                if (!$item['local_type']) {
                    $item['local_type_desc'] = '自营店';
                } elseif ($item['local_type'] == '1') {
                    $item['local_type_desc'] = '加盟店';
                } elseif ($item['local_type'] == '2') {
                    $item['local_type_desc'] = '租赁店';
                }
            }
        }
        $this->assign('list', $data);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 可上架商品管理
     */
    public function shelves()
    {
        $p = max(I('param.p'), 1);
        $num = max(I('param.num'), 20);
        $barcode = I('param.barcode', '', 'trim');
        $name = I('param.name', '', 'trim');
        $order_field = I('param.order_field');
        $order = I('param.order', 'asc');
        $sunxu = $order == 'asc' ? 'desc' : 'asc';
        $this->assign('order', $sunxu);
        $this->assign('p', $p);
        if ($order_field && $order) {
            $order = "order by c.p_relation_id {$order}" . " ,a.{$order_field} {$order} ";
            $this->assign('order_field', $order_field);
        } else {
            $order = " order by c.p_relation_id desc,a.product_id desc ";
        }
        $where = " where a.disabled='false' ";
        if ($barcode) {
            $where .= " and a.barcode like '%{$barcode}%' ";
            $this->assign('barcode', $barcode);
        }
        if ($name) {
            $where .= " and a.name like '%{$name}%' ";
            $this->assign('name', $name);
        }

        $db = new Model();
        $count = $db->query($this->_sql('count(*)', $where));
        $Page = new \Think\Page($count[0]['count(*)'], $num);
        $show = $Page->show();
        $fields = array(
            'a.product_id pid',
            'a.goods_id gid',
            'a.barcode',
            'a.bn',
            'a.name',
            'a.price',
            'a.cost',
            'a.store',
            'c.p_relation_id',
            'c.guidance_price',
            'a.spec_info',
        );
        $maxPage = ceil($count[0]['count(*)'] / $num);
        if ($p > $maxPage) $p = $maxPage;
        $skip = ($p - 1) * $num;
        $limit = " limit {$skip},{$num} ";
        $list = $db->query($this->_sql($fields, $where, $order, $limit));
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    private function _sql($fields, $where = '', $order = '', $limit = '')
    {
        /**
         * select a.product_id,a.name,a.barcode,a.bn,a.cost,a.price,c.* from sdb_b2c_products a
         * left join
         * (select DISTINCT * from sdb_b2c_product_relation b where b.product_type_id=2) c
         * on a.product_id=c.product_id ORDER BY c.p_relation_id desc,a.product_id desc
         */
        $where = $where . ' and a.disabled="false"';
        if (is_array($fields)) {
            $fields = join(',', $fields);
        }
        return $sql = "select {$fields} from " . C('DB_PREFIX') . "b2c_products a 
        left join (select DISTINCT p_relation_id,guidance_price,product_id from " . C('DB_PREFIX') . "b2c_product_relation b where b.product_type_id=1 ) c 
        on a.product_id = c.product_id {$where} {$order} {$limit}";
    }

    /**
     *修改店铺
     */
    public function storeUpdate()
    {

        $local_id = I('local_id', '', htmlspecialchars());

        $m = M('webpos_local_store a');

        $store = $m->join('sdb_webpos_local_store_login b on a.local_id=b.local_id', 'left')->where(['a.local_id' => $local_id, 'b.type' => 1])->find();

        if (empty($store)) {
            $this->error('修改店铺不存在！', '/manage/store/index');
        }

        if ($_POST) {
            $local_name = I('local_name', '', htmlspecialchars());
            $username = I('username', '', htmlspecialchars());
            $password = I('password', '', htmlspecialchars());
            $password = !empty($password) ? md5($password) : $store['password'];
            $area_text = I('area_text', '', htmlspecialchars());
            $area_dispatch = I('area_dispatch', '', htmlspecialchars());
            $addr = I('addr', '', htmlspecialchars());
            $local_type = I('local_type', '', htmlspecialchars());
            $local_number = I('local_number', '', htmlspecialchars());
            $local_status = I('local_status', '', htmlspecialchars());
            $local_full_name = I('local_full_name', '', htmlspecialchars());
            $business_begintime = I('business_begintime', '', htmlspecialchars());
            $business_endtime = I('business_endtime', '', htmlspecialchars());
            $principal = I('principal', '', htmlspecialchars());
            $local_log = I('local_log', '', htmlspecialchars());
            $local_log = !empty($local_log) ? $local_log[0] : $store['local_log'];
            $local_photo = I('local_photo', '', htmlspecialchars());
            $local_photo = !empty($local_photo) ? $local_photo[0] : $store['local_photo'];
            $store_lnt = I('store_lnt');
            $store_lat = I('store_lat');
            $tel = I('tel', '', htmlspecialchars());
            $mobile = I('mobile', '', htmlspecialchars());
            $ziti_remark = I('ziti_remark', '', htmlspecialchars());
            $province = I('province', '', htmlspecialchars());
            $city = I('city', '', htmlspecialchars());
            $area = I('area', '', htmlspecialchars());
            $myexpress = I('myexpress', '', htmlspecialchars());
            $is_byself = I('is_byself', '', htmlspecialchars());

            $m = M('webpos_local_store');

            $data = [
                'local_name' => $local_name,
                'area_text' => $area_text,
                'area_dispatch' => $area_dispatch,
                'addr' => $addr,
                'local_type' => $local_type,
                'local_number' => $local_number,
                'local_status' => $local_status,
                'local_full_name' => $local_full_name,
                'business_begintime' => $business_begintime,
                'business_endtime' => $business_endtime,
                'principal' => $principal,
                'local_log' => $local_log,
                'local_photo' => $local_photo,
                'store_lnt' => $store_lnt,
                'store_lat' => $store_lat,
                'tel' => $tel,
                'mobile' => $mobile,
                'ziti_remark' => $ziti_remark,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'myexpress' => $myexpress,
                'is_byself' => $is_byself,
            ];

            try {
                M()->execute('begin');

                if ($local_status == 1){
                    if ($province) {
                        $province_id = $this->add_area($province, 1, 0); // 添加省
                    }
                    if ($province_id) {
                        $city_id = $this->add_area($city, 2, $province_id); // 添加市
                    }
                    if ($city_id) {
                        $this->add_area($area, 3, $city_id); // 添加区
                    }
                }
                if ($local_status == 0){
                    if ($province) {
                        $province_id = $this->delete_area($province, 1, 0, $local_id); // 删除省
                    }
                    if ($province_id) {
                        $city_id = $this->delete_area($city, 2, $province_id, $local_id); // 删除省
                    }
                    if ($city_id) {
                        $this->delete_area($area, 3, $city_id, $local_id); // 删除省
                    }
                }

                $local_save = $m->where(['local_id' => $local_id])->save($data);

                $login = true;
                if ($username != $store['username'] || $password != $store['password']) {
                    $login_data = [
                        'username' => $username,
                        'password' => $password,
                    ];
                    $login = M('webpos_local_store_login')->where(['local_id' => $local_id, 'type' => 1])->save($login_data);
                }
                $share = M('weixin_share_logo');
                // 添加店铺logo 为 微信分享店铺小图标
                $shareresult = $share->where(['local_id'=>$local_id])->find();
                if ($shareresult) {
                    $share_data1 = [
                        'id' =>$shareresult['id'],
                        'local_id'=> $local_id,
                        'image' =>$local_log
                    ];
                    $share->save($share_data1);
                }else{
                    $share_data2 = [
                        'local_id'=> $local_id,
                        'image' =>$local_log[0]
                    ];
                    $share->add($share_data2);
                }

                if ($local_save ===false || $login === false) {
                    throw new \Exception(false);
                }
                M()->execute('commit');

                // 清除无用的地址
                $res = $this->del_store_useless_area();

                $this->success('修改成功', U('manage/store/index'));
            } catch (\Exception $e) {
                M()->execute('rollback');
                $this->error('修改失败');
            }
            exit;
        }

        $ak = C('BAIDU_AK');
        $this->assign(['store' => $store, 'ak' => $ak]);
        $this->display();
    }

//    public function deleteArea($local_id, $local_status){
//        if ($local_status == 0) {
//            // 查询下架的店铺
//            $local_store = M("webpos_local_store")->where(['local_id' => $local_id, 'local_status' => '0'])->find();
//            if ($local_store['province']) {
//                //如果店铺下架就删除省市区
//                $webpos_local_store = M("webpos_local_store")->where(['local_id' => ['neq', $local_id], 'local_status' => '1', 'province' => $local_store['province']])->select();
//                if (!$webpos_local_store) {
//                    $area = M('area')->where(['name' => $local_store['province']])->find();
//                }
//            }
//        }
//    }

    /**
     * 新增店铺
     */
    public function storeAdd()
    {

        if ($_POST) {
            // 清除无用的地址
            $this->del_store_useless_area();
            $local_name = I('local_name', '', htmlspecialchars());
            $username = I('username', '', htmlspecialchars());
            $password = I('password', '', htmlspecialchars());
            $area_text = I('area_text', '', htmlspecialchars());
            $area_dispatch = I('area_dispatch', '', htmlspecialchars());
            $addr = I('addr', '', htmlspecialchars());
            $local_type = I('local_type', '', htmlspecialchars());
            $local_number = I('local_number', '', htmlspecialchars());
            $local_status = I('local_status', '', htmlspecialchars());
            $local_full_name = I('local_full_name', '', htmlspecialchars());
            $business_begintime = I('business_begintime', '', htmlspecialchars());
            $business_endtime = I('business_endtime', '', htmlspecialchars());
            $principal = I('principal', '', htmlspecialchars());
            $local_log = I('local_log', '', htmlspecialchars());
            $local_photo = I('local_photo', '', htmlspecialchars());
            $store_lnt = I('store_lnt', '', htmlspecialchars());
            $store_lat = I('store_lat', '', htmlspecialchars());
            $tel = I('tel', '', htmlspecialchars());
            $mobile = I('mobile', '', htmlspecialchars());
            $ziti_remark = I('ziti_remark', '', htmlspecialchars());
            $province = I('province', '', htmlspecialchars());
            $city = I('city', '', htmlspecialchars());
            $area = I('area', '', htmlspecialchars());
            $myexpress = I('myexpress', '', htmlspecialchars());
            $is_byself = I('is_byself', '', htmlspecialchars());
            $m = M('webpos_local_store');

            $data = [
                'local_name' => $local_name,
                'area_text' => $area_text,
                'area_dispatch' => $area_dispatch,
                'addr' => $addr,
                'local_type' => $local_type,
                'local_number' => $local_number,
                'local_status' => $local_status,
                'local_full_name' => $local_full_name,
                'business_begintime' => $business_begintime,
                'business_endtime' => $business_endtime,
                'principal' => $principal,
                'local_log' => $local_log[0],
                'local_photo' => $local_photo[0],
                'store_lnt' => $store_lnt,
                'store_lat' => $store_lat,
                'tel' => $tel,
                'mobile' => $mobile,
                'ziti_remark' => $ziti_remark,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'myexpress' => $myexpress,
                'is_byself' => $is_byself,
            ];

            try {
                M()->execute('begin');

                if ($local_status == 1){
                    $province_id = $this->add_area($province, 1, 0); // 添加省
                    $city_id = $this->add_area($city, 2, $province_id); // 添加市
                    $this->add_area($area, 3, $city_id); // 添加区
                }

                $local_id = $m->add($data);
                $login_data = [
                    'local_id' => $local_id,
                    'username' => $username,
                    'password' => md5($password),
                ];
                $login = M('webpos_local_store_login')->add($login_data);
                // 添加店铺logo 为 微信分享店铺小图标
                $share_data2 = [
                    'local_id'=> $local_id,
                    'image' =>$local_log[0]
                ];
                $weixinshare = M('weixin_share_logo')->add($share_data2);
                if ($local_id === false || $login === false || $weixinshare===false) {
                    throw new \Exception(false);
                }


                M()->execute('commit');
                $this->success('新增成功', U('manage/store/index'));
                exit;
            } catch (\Exception $e) {
                M()->execute('rollback');
                $this->error('新增失败');
            }

        }
        $ak = C('BAIDU_AK');
        $this->assign(['ak' => $ak]);
        $this->display();
    }

    /**
     * 添加地区
     * @param $name 省、市、区的名称
     * @param $grade 级别(1，2，3)
     * @param int $pid 上级id
     * @return mixed 返回id
     * @throws \Exception
     */
    public function add_area($name, $grade, $pid = 0)
    {
        $area = M('area');
        if ($grade == 1) { // 如果是一级
            $areaMsg = $area->where(['name' => $name, 'area_grade' => $grade, 'pid' => $pid])->find();

            if (empty($areaMsg)) {
                $data = [
                    'name' => $name,
                    'area_grade' => $grade,
                    'area_path' => '1',
                    'pid' => '0',
                    'disabled' => 'true'
                ];
                $add_area = $area->add($data); // 添加一级地区
                if ($add_area === false) {
                    throw new \Exception(false);
                }
                return $add_area; // 返回id
            }
            return $areaMsg['id']; // 返回id
        } elseif ($grade == 2) { // 如果是二级
            $areaMsg = $area->where(['name' => $name, 'area_grade' => $grade, 'pid' => $pid])->find();

            if (empty($areaMsg)) {
                $data = [
                    'name' => $name,
                    'area_grade' => $grade,
                    'area_path' => '1,2',
                    'pid' => $pid,
                    'disabled' => 'true'
                ];
                $add_area = $area->add($data); // 添加一级地区
                if ($add_area === false) {
                    throw new \Exception(false);
                }
                return $add_area; // 返回id
            }
            return $areaMsg['id']; // 返回id
        } elseif ($grade == 3) { // 如果是三级
            $areaMsg = $area->where(['name' => $name, 'area_grade' => $grade, 'pid' => $pid])->find();

            if (empty($areaMsg)) {
                $data = [
                    'name' => $name,
                    'area_grade' => $grade,
                    'area_path' => '1,2,3',
                    'pid' => $pid,
                    'disabled' => 'true'
                ];
                $add_area = $area->add($data); // 添加一级地区
                if ($add_area === false) {
                    throw new \Exception(false);
                }
                return $add_area; // 返回id
            }
            return $areaMsg['id']; // 返回id
        } else {
            throw new \Exception(false);
        }

    }

    public function delete_area($name, $grade, $pid = 0, $local_id)
    {

        $area = M('area');
        if ($grade == 1) { // 如果是一级

            $areaMsg = $area->where(['name' => $name, 'area_grade' => $grade, 'pid' => $pid])->find();

            $webpos_local_store = M("webpos_local_store")->where(['local_id' => ['neq', $local_id], 'local_status' => '1', 'province' => $name])->select();
            if ($webpos_local_store){
                return $areaMsg['id']; // 返回id
            }

            if (!empty($areaMsg)) {
                $d_area = $area->where(['id' => $areaMsg['id']])->delete(); // 删除一级地区
                if ($d_area === false) {
                    throw new \Exception(false);
                }

                $areaMsg_b =$area->where(['area_grade' => '2', 'pid' => $areaMsg['id']])->find(); // 查询2级

                if ($areaMsg_b) {
                    $d_area_b = $area->where(['id' => $areaMsg_b['id']])->delete(); // 删除2级地区
                    if ($d_area_b === false) {
                        throw new \Exception(false);
                    }

                    $areaMsg_c =$area->where(['area_grade' => '3', 'pid' => $areaMsg_b['id']])->find(); // 查询3级
                    if ($areaMsg_c) {
                        $d_area_c = $area->where(['id' => $areaMsg_c['id']])->delete(); // 删除3级地区
                        if ($d_area_c === false) {
                            throw new \Exception(false);
                        }
                    }
                }


            }
            return $areaMsg['id']; // 返回id
        } elseif ($grade == 2) { // 如果是二级

            $areaMsg_b = $area->where(['name' => $name, 'area_grade' => $grade, 'pid' => $pid])->find();// 查询2级
            $webpos_local_store = M("webpos_local_store")->where(['local_id' => ['neq', $local_id], 'local_status' => '1', 'province' => $name])->select();
            if ($webpos_local_store){
                return $areaMsg_b['id']; // 返回id
            }
//            echo $area->_sql();
            if ($areaMsg_b){
                $d_area = $area->where(['id' => $areaMsg_b['id']])->delete(); // 删除2级地区
                if ($d_area === false) {
                    throw new \Exception(false);
                }
                $areaMsg_c =$area->where(['area_grade' => '3', 'pid' => $areaMsg_b['id']])->find(); // 查询3级
                if ($areaMsg_c) {
                    $d_area_c = $area->where(['id' => $areaMsg_c['id']])->delete(); // 删除3级地区
                    if ($d_area_c === false) {
                        throw new \Exception(false);
                    }
                }
            }
            return $areaMsg_b['id']; // 返回id
        } elseif ($grade == 3) { // 如果是三级
//            echo 12312;exit;
            $areaMsg_c = $area->where(['name' => $name, 'area_grade' => $grade, 'pid' => $pid])->find();
            $webpos_local_store = M("webpos_local_store")->where(['local_id' => ['neq', $local_id], 'local_status' => '1', 'province' => $name])->select();
            if ($webpos_local_store){
                return $areaMsg_c['id']; // 返回id
            }
//            echo $area->_sql();exit;
//            var_dump($areaMsg_c);exit;
            if ($areaMsg_c) {
                $d_area_c = $area->where(['id' => $areaMsg_c['id']])->delete(); // 删除3级地区
                if ($d_area_c === false) {
                    throw new \Exception(false);
                }
            }
            return $areaMsg_c['id']; // 返回id
        } else {
            throw new \Exception(false);
        }

    }

    public function deleteImg()
    {
        $local_id = I('id', '', htmlspecialchars());
        $type = I('type', '', htmlspecialchars());
        $storeModel = M('webpos_local_store');
        $storeModel->where(['local_id' => $local_id])->save([$type => '']);
        echo 1;

    }

    public function storeDelete()
    {
        $local_id = I('local_id', '', htmlspecialchars());
        try {
            M()->execute('begin');


            $store = M('webpos_local_store')->where(['local_id' => $local_id])->delete();
            $login = M('webpos_local_store_login')->where(['local_id' => $local_id, 'type' => 1])->delete();
            $share = M('weixin_share_logo')->where(['local_id' => $local_id, 'type' => 0])->delete();
            if ($store === false || $login === false|| $share==false) {
                throw new \Exception(false);
            }
            M()->execute('commit');
            $this->success('删除成功', U('manage/store/index'));
        } catch (\Exception $e) {
            M()->execute('rollback');
            $this->error('删除失败');
        }
    }

    public function setCommission()
    {

        //无限分类 查询出平台的所有分类
        $local_id = I('local_id');
        if (!$local_id) {
            $this->error();
        }
        $list = $this->get_categories_tree($local_id);
        $this->assign('list', $list);
        $this->assign('local_id', $local_id);
        $this->display();

    }

    public function saveCommission()
    {
        $storeCommission = M('b2c_store_cat_commission');
        if (!IS_AJAX) {
            $return = array(
                'status' => false,
                'message' => '非法请求',
            );
            $this->ajaxReturn($return);
        }
        $cat_id = I('post.cat_id');
        $num = I('post.num');
        $local_id = I('get.local_id');
        if (!$cat_id || !$local_id) {
            $return = array(
                'status' => false,
                'message' => '参数缺失',
            );
            $this->ajaxReturn($return);
        }
        if (is_array($cat_id)) {
            $db = new Model();
            $db->startTrans();
            foreach ($cat_id as $item) {
                $where = ['local_id' => $local_id, 'cat_id' => $item['cat_id']];
                $exists = $storeCommission->where($where)->count();

                if ($exists) {
                    $res = $storeCommission->where($where)->save(['num' => $item['num']]);
                } else {
                    $res = $storeCommission->add(array_merge($where, ['num' => $item['num']]));
                }
                if ($res === false) {
                    $db->rollback();
                    $return = array(
                        'status' => false,
                        'message' => '失败',
                    );
                    $this->ajaxReturn($return);
                }
            }

            $db->commit();
            $return = array(
                'status' => true,
                'message' => '成功',
            );
            $this->ajaxReturn($return);
            die;
        } else {
            $where = ['local_id' => $local_id, 'cat_id' => $cat_id];
            if ($storeCommission->where($where)->count()) {
                $res = $storeCommission->where($where)->save(['num' => $num]);
            } else {
                $res = $storeCommission->add(array_merge($where, ['num' => $num]));
            }
        }

        if ($res !== false) {
            $return = array(
                'status' => true,
                'message' => '成功',
                'data' => $res,
            );
        } else {
            $return = array(
                'status' => false,
                'message' => '失败',
            );
        }
        $this->ajaxReturn($return);
    }

    private function get_categories_tree($local_id)
    {
           $b2cGoodsCat = M('b2c_goods_cat');
        /*
         判断当前分类中全是是否是底级分类，
         如果是取出底级分类上级分类，
         如果不是取当前分类及其下的子分类
        */
        $count = $b2cGoodsCat->where(['parent_id' => 0, 'disabled' => 'false'])->count();
        if ($count || $parent_id == 0) {
            /* 获取当前分类及其子分类 */
            $res = $b2cGoodsCat->field('cat_id,parent_id,cat_name,disabled')
                ->where(['parent_id' => 0, 'disabled' => 'false'])
                ->order(['p_order' => 'asc', 'cat_id' => 'asc'])
                ->select();

            foreach ($res AS $row) {
                if ($row['disabled'] == 'false') {
                    $cat_arr[$row['cat_id']]['id'] = $row['cat_id'];
                    $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
                    $cat_goodsnum = M('b2c_goods')->where(['cat_id' => $row['cat_id']])->count();
                    if ($cat_goodsnum > 0) {
                        $num = M('b2c_store_cat_commission')->where(['local_id' => $local_id, 'cat_id' => $row['cat_id']])->getField('num');
                        if ($num === NULL) {
                            $num = C('DEFAULT_COMMISSION');
                        }
                        $cat_arr[$row['cat_id']]['num'] = $num;
                    }
                    if (isset($row['cat_id']) != NULL) {
                        $cat_arr[$row['cat_id']]['cat_id'] = $this->get_child_tree($row['cat_id'], $local_id);
                    }
                }
            }
        }
        if (isset($cat_arr)) {
            return $cat_arr;
        }
    }

    private function get_child_tree($tree_id = 0, $local_id)
    {
        $b2cGoodsCat = M('b2c_goods_cat');
        $three_arr = array();

        $count = $b2cGoodsCat->where(['parent_id' => $tree_id, 'disabled' => 'false'])->count();
        if ($count || $tree_id == 0) {
            $res = $b2cGoodsCat->field('cat_id,parent_id,cat_name,disabled')
                ->where(['parent_id' => $tree_id, 'disabled' => 'false'])
                ->order(['p_order' => 'asc', 'cat_id' => 'asc'])
                ->select();

            foreach ($res AS $row) {
                if ($row['disabled'] == 'false') {

                    $three_arr[$row['cat_id']]['id'] = $row['cat_id'];
                    $three_arr[$row['cat_id']]['name'] = $row['cat_name'];

                    $cat_goodsnum = M('b2c_goods')->where(['cat_id' => $row['cat_id']])->count();
                    if ($cat_goodsnum > 0) {
                        $num = M('b2c_store_cat_commission')->where(['local_id' => $local_id, 'cat_id' => $row['cat_id']])->getField('num');
                        if ($num === NULL) {
                            $num = C('DEFAULT_COMMISSION');
                        }
                        $three_arr[$row['cat_id']]['num'] = $num;
                    }
                    if (isset($row['cat_id']) != NULL) {
                        $three_arr[$row['cat_id']]['cat_id'] = $this->get_child_tree($row['cat_id'], $local_id);
                    }
                }


            }
        }
        return $three_arr;
    }

    // 删除无用地址
    public function del_store_useless_area(){
        $store = M('webpos_local_store')->where(['is_self' => ['neq',1], 'local_id' => ['neq', 26]])->select();

        $m_area =M('area');
        $id_arr = [];
        for ($i=0; $i<count($store); $i++) {
            $province = $store[$i]['province'];
            $city = $store[$i]['city'];
            $area = $store[$i]['area'];
            // 查询省份
            $area_province = $m_area->where(['pid' => 0, 'area_grade' => 1,'name' => $province])->find();
            $area_city = $m_area->where(['pid' => $area_province['id'], 'area_grade' => 2,'name' => $city])->find();
            $area_area = $m_area->where(['pid' => $area_city['id'], 'area_grade' => 3,'name' => $area])->find();
            array_push($id_arr, $area_province['id'], $area_city['id'], $area_area['id']);
        }
        $id_arr = array_unique($id_arr);
        $id_arr = implode(',', $id_arr);
        $res = $m_area->where(["id" => ["not in", $id_arr]])->delete();

    }

}